package com.oa.controller;

import com.oa.back.ResponseBack;
import com.oa.common.annotion.Interceptor;
import com.oa.common.base.Pageable;
import com.oa.common.enums.CodeStatus;
import com.oa.common.enums.apps.AppsSearchStatus;
import com.oa.entities.flows.Apps;
import com.oa.service.AppsService;
import com.oa.service.AppsTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/manage/apps")
public class AppsController extends BaseController {

    private static final Logger logger= LoggerFactory.getLogger(AppsController.class);

    @Autowired
    private AppsService appsService;

    @Autowired
    private AppsTypeService typeService;


    @RequestMapping(value = "type.html",method = RequestMethod.GET)
    public String type(ModelMap modelMap, String key, Pageable pageable,Integer type){
        modelMap.addAttribute("key",key);
        modelMap.addAttribute("page",typeService.findPage(pageable,key,type));
        modelMap.addAttribute("pageable",pageable);
        modelMap.addAttribute("type",type);
        return "/admin/apps/type";
    }

    @RequestMapping(value = "submit",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "提交公文的接口")
    public ResponseBack<Long> submit(String params) {
        try {
            return appsService.submit(params);
        } catch (RuntimeException ex) {
            logger.error("提交公文接口异常:", ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }

    @RequestMapping(value = "add.html",method = RequestMethod.GET)
    public String add(ModelMap modelMap,Long id){
        Apps apps= appsService.get(id);
        modelMap.addAttribute("apps",apps);
        modelMap.addAttribute("template",appsService.getTemplate(
                apps.getAppType().getId(),id));
        return "/admin/apps/add";
    }

    @RequestMapping(value = "transect.html",method = RequestMethod.GET)
    public String transect(ModelMap modelMap,String key,Pageable pageable,Integer type){
        if (type==null)
            type=0;
        modelMap.addAttribute("key",key);
        modelMap.addAttribute("pageable",pageable);
        modelMap.addAttribute("currentType",type);
        modelMap.addAttribute("page",appsService.findPage(pageable,key,type));
        modelMap.addAttribute("types", AppsSearchStatus.values());
        return "/admin/apps/transect";
    }

    @RequestMapping(value = "transect",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "办理公文的接口")
    public ResponseBack<String> transect(String params){
        try{
            return appsService.transect(params);
        }catch (RuntimeException ex){
            logger.error("办理公文接口的异常:",ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }

    @RequestMapping(value = "view.html",method = RequestMethod.GET)
    public String view(ModelMap modelMap,Long id){
        Apps apps= appsService.get(id);
        modelMap.addAttribute("apps",apps);
        modelMap.addAttribute("template",appsService.getViews(apps.getAppType().getId(),apps.getId()));
        return "/admin/apps/views";
    }

    @RequestMapping(value = "core.html",method = RequestMethod.GET)
    public String core(ModelMap modelMap,Long id){
        Apps apps= appsService.get(id);
        modelMap.addAttribute("apps",apps);
        return "/admin/apps/core";
    }

    @RequestMapping(value = "core",method = RequestMethod.POST)
    @ResponseBody
    @Interceptor(actionName = "转交下一步的接口")
    public ResponseBack<String> core(Long appsId,Long personId,int type){
        try{
            return appsService.core(appsId, personId, type);
        }catch (RuntimeException ex){
            logger.error("交下一步接口的异常:",ex);
            return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
        }
    }
}
